﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.Core.Model;
using EzDesk.Utility;

namespace EzDesk.Apps.ServiceImpl.ProjectModule.History
{

    public class TeamHistoryRecorder : HistoryRecorderBase
    {
        private ProjectHistory _projectHistory = new ProjectHistory();

        public override ProjectHistory ProjectHistory
        {
            get { return _projectHistory; }
        }

        protected string OldMemberstring { get; set; }
        protected List<SimpleUser> OldUserList { get; set; }
        protected string NewMemberstring { get; set; }
        protected List<SimpleUser> NewUserList { get; set; }
        protected Guid? SenderId { get; set; }
        protected SimpleUser Sender { get; set; }
        protected Guid ProjectId { get; set; }

        // todo: 改成直接传两个list进来
        public TeamHistoryRecorder(Guid projectId, string oldMemberstring, string newMemberstring, Guid? senderId)
        {
            initializeHistory(projectId, oldMemberstring, newMemberstring, senderId, "TeamHistory");
        }

        private void initializeHistory(Guid projectId, string oldMemberstring, string newMemberstring, Guid? senderId, string categoryName)
        {
            ProjectId = projectId;
            OldMemberstring = oldMemberstring;
            NewMemberstring = newMemberstring;
            SenderId = senderId;

            OldUserList = EzApiClient.GetSimpleUserInfos(OldMemberstring);
            NewUserList = EzApiClient.GetSimpleUserInfos(NewMemberstring);
            if (senderId.HasValue)
            {
                Sender = EzApiClient.GetSimpleUserById(SenderId.Value);
            }

            _projectHistory = new ProjectHistory()
                                  {
                                      Id = Guid.NewGuid(),
                                      CreateOn = DateTime.Now,
                                      HistoryType = HistoryType.Team,
                                      ProjectId = ProjectId,
                                      SenderId = senderId,
                                      LinkUrl = GetTeamLinkUrl(),
                                      Content = GetTeamContent(),
                                      CategoryName = categoryName,
                                  };
        }

        protected string GetTeamContent()
        {
            var insertMemberList = NewUserList.Where(p => !OldUserList.ContainUser(p)).ToList();
            var removeMemberList = OldUserList.Where(p => !NewUserList.ContainUser(p)).ToList();

            //todo: 重构这个代码。。。

            var sb = new StringBuilder();
            int i = 0;
            foreach (var simpleUser in insertMemberList)
            {
                if (i < 3)
                {
                    if (i != 0) sb.Append(", ");
                    sb.Append(simpleUser.Name);
                }
                else
                {
                    sb.AppendFormat(" 等{0}人", insertMemberList.Count);
                    break;
                }
                i++;
            }
            if (i > 0)
            {
                sb.Append(" 添加入项目团队");
            }
            var insertMemberString = sb.ToString();

            sb = new StringBuilder();
            i = 0;
            foreach (var simpleUser in removeMemberList)
            {
                if (i < 3)
                {
                    if (i != 0) sb.Append(", ");
                    sb.Append(simpleUser.Name);
                }
                else
                {
                    sb.AppendFormat(" 等{0}人", removeMemberList.Count);
                    break;
                }
                i++;
            }
            if (i > 0)
            {
                sb.Append(" 从项目团队中移除");
            }
            var removeMemberString = sb.ToString();

            return "{0} 将 {1} {2}".FormatString(Sender.Name, insertMemberString, removeMemberString);
        }

        private string GetTeamLinkUrl()
        {
            // todo: 这个workitem有可能会显示不出来的，因为这个workitem并不一定是“自己的”Workitem
            return "/project/projectMembers/{0}".FormatString(ProjectId.ToString());
        }
    }
}